package chpt10;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class ListTableDemo extends JFrame {
    private DefaultListModel<String> listModel;
    private JList<String> list;

    private DefaultTableModel tableModel;
    private JTable table;

    public ListTableDemo() {
        setTitle("JList与JTable示例");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLayout(new BorderLayout());

        // 创建一个JList对象
        listModel = new DefaultListModel<>();
        listModel.addElement("小明");
        listModel.addElement("韩梅梅");
        listModel.addElement("李雷");
        listModel.addElement("张三");
        listModel.addElement("李四");
        listModel.addElement("王五");
        listModel.addElement("赵六");
        listModel.addElement("钱七");
        listModel.addElement("孙八");
        listModel.addElement("周九");
        list = new JList<>(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        // 添加鼠标双击事件监听器
        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    String selectedName = list.getSelectedValue();
                    if (selectedName != null && !isNameExist(selectedName)) {
                        String gender = generateRandomGender();
                        String birthDate = generateRandomBirthDate();
                        tableModel.addRow(new Object[]{selectedName, gender, birthDate});
                    }
                }
            }
        });
        
        // 创建一个JTable对象
        String[] columnNames = {"姓名", "性别", "出生年月"};
        Object[][] data = {};
        tableModel = new DefaultTableModel(data, columnNames);
        table = new JTable(tableModel);
        
        // 创建一个下拉框
        JComboBox<String> genderComboBox = new JComboBox<>();
        genderComboBox.addItem("男");
        genderComboBox.addItem("女");

        // 将下拉框设置为性别列的单元格编辑器
        TableColumn genderColumn = table.getColumnModel().getColumn(1);
        genderColumn.setCellEditor(new DefaultCellEditor(genderComboBox));

        // 创建一个按钮
        JButton addButton = new JButton("从JList添加到JTable");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedName = list.getSelectedValue();
                if (selectedName != null && !isNameExist(selectedName)) {
                    String gender = generateRandomGender();
                    String birthDate = generateRandomBirthDate();
                    tableModel.addRow(new Object[]{selectedName, gender, birthDate});
                }
            }
        });

        // 创建一个删除按钮
        JButton deleteButton = new JButton("删除选中行");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow();
                if (selectedRow != -1) {
                    tableModel.removeRow(selectedRow);
                }
            }
        });

        // 将JList、JTable和按钮添加到窗口中
        JScrollPane listScrollPane = new JScrollPane(list);
        JScrollPane tableScrollPane = new JScrollPane(table);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        add(listScrollPane, BorderLayout.WEST);
        add(tableScrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    private boolean isNameExist(String name) {
        for (int i = 0; i < tableModel.getRowCount(); i++) {
            String existingName = (String) tableModel.getValueAt(i, 0);
            if (existingName.equals(name)) {
                return true;
            }
        }
        return false;
    }

    private String generateRandomGender() {
        List<String> genders = new ArrayList<>();
        genders.add("男");
        genders.add("女");
        Random random = new Random();
        int index = random.nextInt(genders.size());
        return genders.get(index);
    }

    private String generateRandomBirthDate() {
        Random random = new Random();
        int year = random.nextInt(20) + 1980;
        int month = random.nextInt(12) + 1;
        int day = random.nextInt(28) + 1;
        return year + "-" + month + "-" + day;
    }

    public static void main(String[] args) {
        new ListTableDemo();
    }
}